<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../css/style.css">
    <style>
        .time {
            font-size: 13px;
            color: #999;
        }

        .bottom {
            margin-top: 13px;
            line-height: 12px;
        }

        .button {
            padding: 0;
            float: right;
        }

        .image {
            width: 75%;
            display: block;
        }

        .clearfix:before,
        .clearfix:after {
            display: table;
            content: "";
        }

        .clearfix:after {
            clear: both
        }
    </style>

</head>
<body>
<div id="app">
    <el-menu
            class="el-menu-demo"
            mode="horizontal"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b">
        <el-menu-item index="1">
            <el-link href="index.html" :underline="false">博客首页</el-link>
        </el-menu-item>
        <el-menu-item index="2">下载中心</el-menu-item>
        <el-menu-item index="3">消息中心</el-menu-item>
        <el-menu-item index="4">
            <el-link href="editor.html" :underline="false">创作中心</el-link>
        </el-menu-item>
        <el-menu-item index="5">
            <el-row class="demo-autocomplete">
                <el-col :span="12">
                    <el-autocomplete
                            class="inline-input"
                            v-model="pagination.queryString"
                            :fetch-suggestions="querySearch(pagination.queryString)"
                            placeholder="请输入问题"
                            :trigger-on-focus="false"
                            @select="handleSelect"
                            style="padding-bottom: 10px;width: 300px"
                    ></el-autocomplete>
                    <el-button style="margin-bottom: 5px" @click="findPage(1)" class="dalfBut">搜索</el-button>
                </el-col>
            </el-row>
        </el-menu-item>

        <div class="right-menu" v-if="formData.username!==null&&(formData.username || []).length>0">
            <el-dropdown class="avatar-container right-menu-item" trigger="click">
                <div class="avatar-wrapper">
                    <img src="../img/logo.jpg" class="user-avatar">
                    {{formData.username}}
                </div>
                <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item divided>
                        <span style="display:block;">修改密码</span>
                    </el-dropdown-item>
                    <el-dropdown-item divided>
                        <span style="display:block;">
                            <a href="http://localhost:9002/sec/logout">退出</a>
                        </span>
                    </el-dropdown-item>
                </el-dropdown-menu>
            </el-dropdown>
        </div>
        <div class="right-menu" v-else-if="formData.username===null||(formData.username || []).length===0">
            <el-button style="margin-bottom: 10px" @click="handleLoginForm()" class="dalfBut">登录</el-button>
            <el-button style="margin-bottom: 10px" @click="handleRegisterForm()" class="dalfBut">注册</el-button>
        </div>
    </el-menu>

    <el-col :span="12" style="width: 100px ">
        <el-menu
                class="el-menu-vertical-demo"
                background-color="#545c64"
                text-color="#fff"
                active-text-color="#ffd04b">
            <el-menu-item index="1">
                <span slot="title">Java</span>
            </el-menu-item>
            <el-menu-item index="2">
                <span slot="title">C++</span>
            </el-menu-item>
            <el-menu-item index="3">
                <span slot="title">MySql</span>
            </el-menu-item>
            <el-menu-item index="4">
                <span slot="title">redis</span>
            </el-menu-item>
        </el-menu>
    </el-col>

    <el-row style="margin-left: 100px">
        <el-col :span="8" v-for="(o, index) in 10" :key="o" :offset="index > 0 ? 10 : 0"
                style="width: 250px;margin-left: 50px;margin-bottom: 30px">
            <el-card shadow="hover" :body-style="{ padding: '10px'}" style="width: 250px;height: 250px">
                <div align="center">
                    <img src="https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png"
                         class="image">
                </div>
                <div style="padding: 10px;">
                    <span>好吃的汉堡</span>
                    <div class="bottom clearfix">
                        <time class="time">2021-2-11</time>
                        <el-button type="text" class="button">查看</el-button>
                    </div>
                </div>
            </el-card>
        </el-col>
    </el-row>

    <div class="pagination-container">
        <el-pagination
                class="pagiantion"
                @current-change="handleCurrentChange"
                :current-page="pagination.currentPage"
                :page-size="pagination.pageSize"
                layout="total, prev, pager, next, jumper"
                :total="pagination.total">
        </el-pagination>
    </div>

    <div class="register-form">
        <el-dialog title="用户注册" :visible.sync="dialogFormVisible4Register">
            <el-form ref="dataRegisterForm" :model="formData" :rules="rules" label-position="right"
                     label-width="100px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="用户名" prop="username">
                            <el-input v-model="formData.username"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="密码" prop="password">
                            <el-input type="password" v-model="formData.password"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="确认密码" prop="confirmPassword">
                            <el-input type="password" v-model="formData.confirmPassword"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="手机号" prop="phoneNumber">
                            <el-input v-model="formData.phoneNumber"/>
                        </el-form-item>
                    </el-col>
                    <el-button :disabled="validateCodeButtonDisabled" style="margin-top: 10px;margin-right: 200px"
                               type="text"
                               class="button" @click="sendValidateCode('REGISTER')">{{validateCodeButtonValue}}
                    </el-button>
                </el-row>
                <el-col :span="12">
                    <el-form-item label="验证码" prop="securityCode">
                        <el-input v-model="formData.validateCode"/>
                    </el-form-item>
                </el-col>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible4Register = false">取消</el-button>
                <el-button type="primary" @click="handleRegister()">注册</el-button>
            </div>
        </el-dialog>
    </div>

    <div class="login-form">
        <el-dialog title="用户登录" :visible.sync="dialogFormVisible4Login">
            <template>
                <el-tabs v-model="activeName" type="card">
                    <el-tab-pane label="用户名登录" name="first" @click="resetForm">
                        <el-form ref="dataLoginForm" :model="formData" :rule="rules" label-position="right"
                                 label-width="100px">
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="用户名">
                                        <el-input v-model="formData.username"/>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="密码">
                                        <el-input v-model="formData.password"/>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </el-form>
                        <div style="float: right">
                            <el-button @click="dialogFormVisible4Login = false">取消</el-button>
                            <el-button type="primary" @click="handleLogin(1)">登录</el-button>
                        </div>
                    </el-tab-pane>
                    <el-tab-pane label="验证码登录" name="second" @click="resetForm">
                        <el-form ref="dataLoginForm" :model="formData" :rule="rules" label-position="right"
                                 label-width="100px">
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="手机号">
                                        <el-input v-model="formData.phoneNumber"/>
                                    </el-form-item>
                                </el-col>
                                <el-button :disabled="validateCodeButtonDisabled"
                                           style="margin-top: 10px;margin-right: 200px" type="text"
                                           class="button" @click="sendValidateCode('LOGIN')">{{validateCodeButtonValue}}
                                </el-button>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="验证码">
                                        <el-input v-model="formData.validateCode"/>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </el-form>
                        <div style="float: right">
                            <el-button @click="dialogFormVisible4Login = false">取消</el-button>
                            <el-button type="primary" @click="handleLogin(0)">登录</el-button>
                        </div>
                    </el-tab-pane>
                </el-tabs>
            </template>
        </el-dialog>
    </div>

</div>
</body>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>

<script src="../js/axios-0.18.0.js"></script>
<script src="../js/validate.js"></script>
<script>
    axios.defaults.withCredentials = true;
    new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            validateCodeButtonDisabled: false,
            validateCodeButtonValue: "发送验证码",
            activeName: 'first',//添加/编辑窗口Tab标签名称
            currentDate: new Date(),
            restaurants: [],
            state: "",
            formData: {
                username: "",
                password: "",
                confirmPassword: "",
                phoneNumber: "",
                validateCode: ""
            },//表单数据
            dialogFormVisible4Register: false,//增加表单是否可见
            dialogFormVisible4Login: false,//编辑表单是否可见
            rules: {//校验规则
                username: [{required: true, message: '用户名为必填项', trigger: 'blur'}],
                password: [{required: true, message: '密码为必填项', trigger: 'blur'}, {
                    trigger: 'blur'
                }],
                confirmPassword: [{required: true, message: '确认密码为必填项', trigger: 'blur'}, {
                    trigger: 'blur'
                }]
            },
            backend_url: "http://localhost:9002"
        },

        created() {
            // 发送请求获取当前登录用户的用户名
            axios.get(this.backend_url + "/user/getUsername",
               ).then((response) => {
                if (response.data.flag) {
                    //更新模型中的信息
                    this.username = response.data.result;
                } else {
                    //获取用户信息失败，重新登录
                    window.location.href = backend_url + "/sec/logout";
                }
            }).catch((error) => {
                console.log(error);
                this.$message.error("网络异常");
            });
        },
        methods: {
            querySearch(state) {
                // axios.get(this.backend_url + "/article/querySearch").then((response) => {
                //     if (response.data.flag) {
                //         //更新模型中的信息
                //
                //     } else {
                //         //失败消息提醒
                //         this.$message.error(response.data.message);
                //     }
                // }).catch((error) => {
                //     console.log(error);
                //     this.$message.error("网络异常");
                // });
            },
            findPage(pageNo) {

            },
            //发送验证码
            sendValidateCode(type) {
                //获取用户输入的手机号
                let phoneNumber = this.formData.phoneNumber;
                //校验手机号输入是否正确
                if (!checkTelephone(phoneNumber)) {
                    this.$message.error('请输入正确的手机号');
                    return false;
                }
                axios.post(this.backend_url + "/validateCode/send?type=" + type + "&phoneNumber=" + phoneNumber).then((response) => {
                    // if (response.data.flag) {
                    //     let nums = 30;
                    //     this.validateCodeButtonDisabled = true;//将按钮置为不可点击
                    //     for (let i = nums; i >= 0; i--) {
                    //         sleep(1000);
                    //         update(i)
                    //     }
                    //     function sleep(NumMillis) {
                    //         let now = new Date();
                    //         let exitTime = now .getTime() + NumMillis;
                    //         while (true) {
                    //             now = new Date();
                    //             if (now.getTime() > exitTime)
                    //                 return;
                    //         }
                    //     }
                    //     function update(num) {
                    //         if (num > 0) {
                    //             this.validateCodeButtonValue = num + '秒后重新获取';
                    //         } else {
                    //             this.validateCodeButtonDisabled = false;
                    //             this.validateCodeButtonValue = '重新获取验证码';
                    //             nums = 30; //重置时间
                    //         }
                    //     }
                    // }
                    if (!response.data.flag) {
                        //验证码发送失败
                        this.$message.error('验证码发送失败，请检查手机号输入是否正确');
                    }
                });
            },
            handleRegister() {
                this.$refs["dataRegisterForm"].validate((valid) => {
                    if (valid) {
                        axios.post(this.backend_url + "/user/register", this.formData)
                            .then((response) => {
                                if (response.data.flag) {
                                    //更新模型中的信息
                                    this.dialogFormVisible4Register = false;
                                    this.$message.success(response.data.message);
                                    this.resetForm();
                                } else {
                                    //失败消息提醒
                                    this.$message.error(response.data.message);
                                }
                            }).catch((error) => {
                            console.log(error);
                            this.$message.error("网络异常");
                        });
                    }
                });
            },
            handleLogin(flag) {
                this.$refs["dataLoginForm"].validate((valid) => {
                    if (valid) {
                        axios.post(this.backend_url + "/user/login/" + flag, this.formData)
                            .then((response) => {
                                if (response.data.flag) {
                                    //更新模型中的信息
                                    this.dialogFormVisible4Login = false;
                                    this.$message.success(response.data.message);
                                    this.resetForm();
                                } else {
                                    //失败消息提醒
                                    this.$message.error(response.data.message);
                                }
                            }).catch((error) => {
                            console.log(error);
                            this.$message.error("网络异常");
                        });
                    }
                });
            },

            handleSelect(item) {
                console.log(item);
            },
            // 重置表单
            resetForm() {
                this.formData = {};
            },
            // 弹出注册窗口
            handleRegisterForm() {
                this.resetForm();
                this.dialogFormVisible4Register = true;
            },
            // 弹出登录窗口
            handleLoginForm() {
                this.resetForm();
                this.dialogFormVisible4Login = true;
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
        }
    });
</script>
</html>